package au.com.carboncycle.gb.engine.graphics;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.Image;
import java.awt.Point;
import java.awt.Polygon;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.geom.Path2D;
import java.awt.geom.PathIterator;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;

import au.com.carboncycle.gb.engine.Screen;
import au.com.carboncycle.gb.engine.entities.Sprite;

/**
 * Delegate to all graphical tasks required by the engine.  Abstracts out the rendering engine - perhaps
 * for something different down the track?
 * 
 * @author Brendan
 *
 *TODO: This should probably be a level renderer or something - not a general renderer
 *TODO: need  a separate background renderer when the camera starts to scroll?
 */
public class GBRenderer implements ImageObserver {

	private Graphics2D canvasGraphics, frameGraphics;
	private Image canvas;
	private GraphicsConfiguration graphicsConfig;
	private Rectangle size;
	
	public GBRenderer(GraphicsConfiguration graphicsConfig, 
						Rectangle size, 
						Image canvas,
						Graphics2D frameGraphics) {
		
		this.graphicsConfig = graphicsConfig;
		this.canvas = canvas;
		this.canvasGraphics = (Graphics2D)this.canvas.getGraphics();
		this.size = size;
		this.frameGraphics = frameGraphics;
		this.canvasGraphics.setColor(Color.WHITE);
	}

	public void startRender (Screen screen) {
		screen.render(this); //double dispatch, essentially
	}

	public void drawString(String msg, int size, int x, int y, Font font, Color color) {
		canvasGraphics.setFont(font);
		canvasGraphics.setColor(color);
		canvasGraphics.drawString(msg, x, y);
	}

	public void drawBackground(BufferedImage background) {
		canvasGraphics.drawImage(background, 0, 0, this);
	}

	
	
	@Override
	public boolean imageUpdate(Image img, int infoflags, int x, int y,	int width, int height) {
		return false;
	}

	public void drawEntity(Image bufferedImage, Point playerPos) {
		canvasGraphics.drawImage(bufferedImage, playerPos.x, playerPos.y, this);
		
	}
	
	public void finishRendering() {
		frameGraphics.drawImage(canvas, 0, 0, null);
	}
	
	/**
	 * Just used to outline areas on the screen for debugging purposes
	 * @param shape
	 */
	public void drawShape(Shape shape) {
		canvasGraphics.setPaint(Color.WHITE);
		canvasGraphics.setStroke(new BasicStroke(5.0f));
		canvasGraphics.draw(shape);
	}
	
	public void drawPath(Path2D path) {
		canvasGraphics.setPaint(Color.WHITE);
		canvasGraphics.setStroke(new BasicStroke(5.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
		canvasGraphics.draw(path);
	}
	

	public void drawEntity(Sprite heroSprite) {
		drawEntity(heroSprite.getCurrentImage(), heroSprite.getCurrentPosition());
	}
	
	
}
